"""
按周期计算是否新高新低，简单方法
"""
from research.calcor import calcor_base
class isnhnl_bybar(calcor_base):
    def __init__(self,timeperiod=10,mintimeperiod=None,indays=1,flags=[2,-2]):
        super().__init__(timeperiod=timeperiod,mintimeperiod=mintimeperiod,datatype="bar",inputs=["high","low"])
        self.indays=indays
        self.flags=flags
    def calc(self):
        highs=self.hisdata["high"]
        lows=self.hisdata["low"]
        max0 = max(highs)
        min0 = min(lows)
        rst = 0
        if max0 in highs[-self.indays:]:
            rst = self.flags[0]
        elif min0 in lows[-self.indays:]:
            rst = self.flags[-1]
        return rst

class isnhnl_bybar_old:
    def __init__(self,timeperiod=10):
        self.timeperiod=timeperiod
        self.data=[[],[]]
        self.timekey = None
    def oncalc(self,b,timekey=None):
        rst=None
        if b is not None:
            timekey=b["timekey"]
            high=b["high"]
            low=b["low"]
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[0][-1] = high
                    self.data[1][-1] = low
                else:
                    self.data[0].append(high)
                    self.data[1].append(low)
                    self.timekey = timekey
            else:
                self.data[0].append(high)
                self.data[1].append(low)
            if len(self.data[0])>self.timeperiod:
                self.data[0].pop(0)
                self.data[1].pop(0)
            if len(self.data[0])==self.timeperiod:
                if max(self.data[0])==high:
                    rst=2
                elif min(self.data[1])==low:
                    rst=-2
                else:
                    rst=0
        return rst


